<?php

namespace App\Admin\Controllers;

use App\Models\VisitorRecord;
use App\Models\DeepNew;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Controllers\AdminController;

class VisitorRecordsController extends AdminController
{
    protected $title = '用户浏览记录';
    
    protected function grid()
    {
        $grid = new Grid(new VisitorRecord());
		$grid->model()->orderByDesc('id')->paginate(50);
	    
	    
	  	$grid->column('id', 'ID');
	  	$grid->column('visitor_id', '用户ID');
	  	$grid->column('visit_url', '浏览页面');
	  	$grid->column('title', '浏览页面标题')->display(function () {
	    	if(strstr($this->visit_url,'detail')){

	    		$id =explode("/",$this->visit_url);
	    		$id = end($id);
	    		$title = DeepNew::find($id)->title_zn;
	    	}else{

	    		$title = "";

	    	}
	    	return $title;
	    });

	  	 $grid->column('stayduration', '浏览时长');
	    $grid->column('created_at', '浏览开始时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });
	   	$this->gridActions($grid);
	   	$this->gridFilter($grid);
        return $grid;
    }
    
   	protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			
			$actions->disableEdit();
			$actions->disableView();
			/*$actions->add(new NewsBriefCreate());
			$actions->add(new NewsImageCheck());*/
			//$actions->add(new RefreshBlindBoxRate());
		});

	}

	 protected function gridFilter(Grid $grid)
    {
	    $grid->filter(function (Grid\Filter $filter) {
		    $filter->expand();
			$filter->between('created_at','创建时间')->datetime();
			
	    	
	    });


    }

}
